var fs = require("fs");
var path = require("path");

var renameVideos = function(dirName, videoName, suffix, digitsOrder) {
  if (!videoName) {
    console.error("请输入视频名称");
    return;
  }
  suffix = suffix ? suffix : "mp4";
  digitsOrder = digitsOrder ? digitsOrder : 0;
  var re = new RegExp("\\d\\d\\S*." + suffix + "$", "g");
  fs.readdir(dirName, function(err, files) {
    if (err) {
      return console.error(err);
    }
    files.forEach(function(file) {
      if (file.match(re)) {
        var orders = file.match(/\d\d/g);
        if (orders) {
          if (orders.length > 1 && digitsOrder === 0) {
            console.error("不能确定视频集数");
            console.log(file + ": " + orders.join(", "));
            return;
          } else {
            var newName = videoName + orders[digitsOrder] + "." + suffix;
            console.log(file + " 重命名-> " + newName);
            fs.renameSync(
              path.join(dirName, file),
              path.join(dirName, newName)
            );
          }
        } else {
          console.error("缺失视频集数");
          return;
        }
      }
    });
  });
};

renameVideos(
  "/Users/xiaopeng/Movies/人人影视/僵尸国度 第4季",
  "僵尸国度 第4季",
  "mp4",
  1
);
